<template>


  <el-container>

    <el-header>
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item>
          <el-button type="primary" @click="userVisible=true;userModel={black: false}">添加</el-button>
        </el-form-item>
      </el-form>

      <el-dialog title="添加用户" :visible.sync="userVisible">
        <el-form :model="userModel">
          <el-form-item label="名称">
            <el-input v-model="userModel.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="账户">
            <el-input v-model="userModel.account" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="密码">
            <el-input v-model="userModel.pwd" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="黑名单">
            <el-switch
              v-model="userModel.black"
              active-color="#13ce66"
              inactive-color="#ff4949"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="userVisible = false;userModel={black:false}">取 消</el-button>
          <el-button type="primary" @click="addUser">确 定</el-button>
        </div>
      </el-dialog>

    </el-header>

    <el-main style="width: 100%">
      <el-table :data="users" border style="width: 100%">
        <el-table-column
          prop="id"
          label="Id">
        </el-table-column>
        <el-table-column
          prop="name"
          label="姓名">
        </el-table-column>
        <el-table-column
          prop="account"
          label="账户">
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="100">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="userVisible=true;userModel=scope.row">查看</el-button>
            <el-button type="text" size="small" @click="delUser(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-main>
  </el-container>


</template>
<script>

  import {md5} from 'hz-admin'

  export default {
    name: "User",
    mounted() {
      this.getUsers();
    },
    data() {
      return {
        users: [],
        userVisible: false,
        userModel: {}
      }
    },
    methods: {
      getUsers() {
        this.$http.get("/back/users").then((data) => {
          this.users = data;
        }).catch(error => {
          console.error(error)
        });
      },
      addUser() {
        this.userModel.pwd = md5.md5(this.userModel.pwd)
        this.$http.post("/back/user", this.userModel).then((data) => {
          this.users = data;
          this.userVisible = false;
        }).catch(error => {
          this.$message.error("添加用户异常！")
        });
      },
      delUser(user) {
        this.$http.delete("/back/delUser?userId=" + user.id).then((data) => {
          this.users = data;
        }).catch(error => {
          this.$message.error("删除用户异常！")
        });
      }
    },
  }
</script>
